package com.surprise.base.util;

import java.util.List;

/**
 * SQLUtils.java
 * @author nick
 * @since 2015年5月10日
 */
public class SQLUtils {
	/**
	 * change ' to \'
	 * 
	 * @param s
	 * @return
	 */
	public static String escapeSql(String s) {
		if (s == null) {
			return null;
		}
		String ret = s.replaceAll("\\\\", "\\\\\\\\");
		ret = ret.replaceAll("'", "\\\\'");
		return ret;
	}

	/**
	 * change % to \% change ' to \'
	 * 
	 * @param s
	 * @return
	 */
	public static String escapeSqlLike(String s) {
		if (s == null) {
			return null;
		}
		String ret = s.replaceAll("\\\\", "\\\\\\\\");
		ret = ret.replaceAll("%", "\\\\%");
		ret = ret.replaceAll("'", "\\\\'");
		return ret;
	}

	/**
	 * 
	 * @param condition
	 * @return
	 */
	public static String toConditionSql(List<String> condition) {
		if (condition == null || condition.size() <= 0) {
			return " ";
		}
		StringBuffer sb = new StringBuffer();
		sb.append(condition.get(0));
		for (int i = 1; i < condition.size(); i++) {
			sb.append(" and " + condition.get(i));
		}
		return sb.toString();
	}
}
